package com.dingwei.superiordiary.mvp.presenter;

import com.dingwei.superiordiary.base.MyBaseBean;
import com.dingwei.superiordiary.bean.FailsBean;
import com.dingwei.superiordiary.bean.ShopCartListBean;
import com.dingwei.superiordiary.custom.myCallback;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.base.URLS;
import com.lihao.baselibrary.base.basePresenter;

import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by lihao on 2017/9/12.
 */

public class ShopCartPresenter extends basePresenter {

    private Contract.ShopCartView shopCartView;

    public ShopCartPresenter(Contract.ShopCartView view) {
        super(view);
        this.shopCartView = view;
    }

    public void getShopCartList() {
        RequestParams rp = new RequestParams(URLS.HOST + URLS.cartList);
        rp.addBodyParameter("sessionid", BaseActivity.value.getToken());
        x.http().post(rp, new myCallback(BaseActivity.context, ShopCartListBean.class, rp) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                ShopCartListBean bean = (ShopCartListBean) baseBean;
                shopCartView.setData(bean);
            }

            @Override
            public void fail(FailsBean baseBean, String result) {
                shopCartView.onFail();
            }
        });
    }

    public void editCartNum(final int storePosition, final int position, String id, final int num) {
        RequestParams rp = new RequestParams(URLS.HOST + URLS.editcartList);
        rp.addBodyParameter("sessionid", BaseActivity.value.getToken());
        rp.addBodyParameter("id", id);
        rp.addBodyParameter("number", num + "");
        x.http().post(rp, new myCallback(BaseActivity.context, MyBaseBean.class, rp) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                shopCartView.editCartNumSuccess(storePosition, position, num);
            }

            @Override
            public void fail(FailsBean baseBean, String result) {
            }
        });
    }

    public void delete(final int storePosition, final int position, String id) {
        RequestParams rp = new RequestParams(URLS.HOST+URLS.deleteCart);
        rp.addBodyParameter("sessionid", BaseActivity.value.getToken());
        rp.addBodyParameter("id", id);
        x.http().post(rp, new myCallback(BaseActivity.context, MyBaseBean.class, rp) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                shopCartView.deleteSuccess(storePosition,position);
            }

            @Override
            public void fail(FailsBean baseBean, String result) {
            }
        });
    }
}
